package cn.wangjie.spark.sources

import org.apache.spark.sql.{DataFrame, Dataset, SparkSession}

/**
 * SparkSQL读取MySQL表中的数据
 */
object SparkSQLMySQL {
  def main(args: Array[String]): Unit = {

    // 构建SparkSession实例对象
		val spark: SparkSession = SparkSession.builder()
			.appName(this.getClass.getSimpleName.stripSuffix("$"))
			// TODO: 设置SparkSQL产生Shuffle时分区数目
			.config("spark.sql.shuffle.partitions", "4")
			.master("local[2]")
			.getOrCreate()
		import spark.implicits._
		
		// TODO: 读取MySQL表中的数据
		val empDF: DataFrame = spark.read
			.format("jdbc")
			.option("driver", "com.mysql.cj.jdbc.Driver")
			.option("url", "jdbc:mysql://node1.itcast.cn:3306/?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true")
			.option("user", "root")
			.option("password", "123456")
			.option("dbtable", "db_test.emp")
			.load()
		empDF.printSchema()
		empDF.show(10, truncate = false)
		
		// 应用结束关闭资源
		spark.stop()
	}
	
}
